#!/usr/bin/env python3
"""
语法检查脚本 - 检查项目中的Python文件是否有语法错误
"""

import ast
import os
import sys
from pathlib import Path

def check_syntax(file_path):
    """检查单个文件的语法"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 尝试解析AST
        ast.parse(content)
        return True, None
    except SyntaxError as e:
        return False, f"语法错误: {e}"
    except Exception as e:
        return False, f"其他错误: {e}"

def main():
    """主函数"""
    print("=" * 50)
    print("Python 语法检查")
    print("=" * 50)
    
    # 要检查的文件列表
    files_to_check = [
        "app.py",
        "page/home.py",
        "components/color_trigger.py",
        "utils/config_manager.py",
        "dialogs/color_picker.py",
        "dialogs/trigger_settings.py",
        "dialogs/smart_detection_dialog.py"
    ]
    
    all_passed = True
    
    for file_path in files_to_check:
        if os.path.exists(file_path):
            print(f"检查 {file_path}...", end=" ")
            passed, error = check_syntax(file_path)
            
            if passed:
                print("✅ 通过")
            else:
                print(f"❌ 失败")
                print(f"   错误: {error}")
                all_passed = False
        else:
            print(f"⚠️  文件不存在: {file_path}")
    
    print("=" * 50)
    if all_passed:
        print("✅ 所有文件语法检查通过！")
    else:
        print("❌ 发现语法错误，请检查上述文件")
    print("=" * 50)
    
    return 0 if all_passed else 1

if __name__ == "__main__":
    sys.exit(main())
